/*=============================================================================

  Library: HMPBundleFrame

  Copyright (c) German Cancer Research Center,
    Division of Medical and Biological Informatics

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

=============================================================================*/


#ifndef SERVICEREGISTRATIONPRIVATE_H
#define SERVICEREGISTRATIONPRIVATE_H

#include <QHash>
#include <QMutex>

#include "ServiceProperties_p.h"
#include "IServiceReference.h"


class BundlePrivate;
class IServiceRegistration;

/**
 * \ingroup BundleFramework
 */
class ServiceRegistrationPrivate
{

protected:

  friend class IServiceRegistration;

  // The ServiceReferencePrivate class holds a pointer to a
  // ServiceRegistrationPrivate instance and needs to manipulate
  // its reference count. This way it can keep the ServiceRegistrationPrivate
  // instance alive and keep returning service properties for
  // unregistered service instances.
  friend class ServiceReferencePrivate;

  /**
   * Reference count for implicitly shared private implementation.
   */
  QAtomicInt ref;

  /**
   * Service or ServiceFactory object.
   */
  QObject* service;

public:

  /**
   * Bundle registering this service.
   */
  BundlePrivate* plugin;

  /**
   * Reference object to this service registration.
   */
  IServiceReference reference;

  /**
   * Service properties.
   */
  ServiceProperties properties;

  /**
   * Bundles dependent on this service. Integer is used as
   * reference counter, counting number of unbalanced getService().
   */
  QHash<QSharedPointer<IBundle>,int> dependents;

  /**
   * Object instances that factory has produced.
   */
  QHash<QSharedPointer<IBundle>, QObject*> serviceInstances;

  /**
   * Is service available. I.e., if <code>true</code> then holders
   * of a ServiceReference for the service are allowed to get it.
   */
  volatile bool available;

  /**
   * Avoid recursive unregistrations. I.e., if <code>true</code> then
   * unregistration of this service has started but is not yet
   * finished.
   */
  volatile bool unregistering;

  /**
   * Lock object for synchronous event delivery.
   */
  QMutex eventLock;

  QMutex propsLock;

  ServiceRegistrationPrivate(BundlePrivate* plugin, QObject* service,
                                const Dictionary& props);

  virtual ~ServiceRegistrationPrivate();

  /**
   * Check if a plugin uses this service
   *
   * @param p Bundle to check
   * @return true if plugin uses this service
   */
  bool isUsedByPlugin(QSharedPointer<IBundle> p);

  virtual QObject* getService();

private:

  Q_DISABLE_COPY(ServiceRegistrationPrivate)

};


#endif // SERVICEREGISTRATIONPRIVATE_H
